package com.casit.base.handler;


import com.casit.base.exception.ResultException;
import com.casit.base.model.ResultModel;
import com.casit.base.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {
    public final static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultModel handle(Exception e){
        logger.error("操作异常",e);
        if(e instanceof ResultException){
            return ResultUtil.fail(((ResultException) e).getCode(),e.getMessage());
        }
        return ResultUtil.fail(-1,e.getMessage());
    }
}
